package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.poi.StringUtils;
import com.bwie.pojo.TbBrand;
import com.bwie.service.TbBrandService;
import com.bwie.mapper.TbBrandMapper;
import com.bwie.utils.Result;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
* @author 39304
* @description 针对表【tb_brand(品牌表)】的数据库操作Service实现
* @createDate 2025-05-23 16:38:10
*/
@Service
public class TbBrandServiceImpl extends ServiceImpl<TbBrandMapper, TbBrand>
    implements TbBrandService{
    @Autowired
    TbBrandMapper tbBrandMapper;

    @Autowired
    HttpServletRequest request;

    @Override
    public Result getBrandList(PageInfoVo pageInfoVo) {
        Page<TbBrand> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());

        QueryWrapper<TbBrand> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(TbBrand::getCreateTime);
        if (StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            wrapper.lambda().like(TbBrand::getBrandName,pageInfoVo.getKeyWord());
        }

        Page<TbBrand> tbBrandPage = tbBrandMapper.selectPage(page, wrapper);

        return Result.ok().setData(tbBrandPage);
    }

    @Override
    public Result addBrand(TbBrand tbBrand) {
        String token = request.getHeader("token");
        if (token == null){
            return Result.error().setMsg("请先登录");
        }

        TbBrand one = tbBrandMapper.selectOne(
                new QueryWrapper<TbBrand>().lambda().eq(TbBrand::getBrandName, tbBrand.getBrandName())
        );
        if (one != null){
            return Result.error().setMsg("品牌已存在");
        }

        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String realName = body.get("realName", String.class);
        tbBrand.setCreateTime(new Date());
        tbBrand.setCreateBy(realName);

        tbBrandMapper.insert(tbBrand);

        return Result.ok();
    }

    @Override
    public Result updateBrandStatus(TbBrand tbBrand) {
        TbBrand brand = tbBrandMapper.selectById(tbBrand.getBrandId());
        if (brand == null){
            return Result.error().setMsg("品牌不存在");
        }

        brand.setBrandStatus(tbBrand.getBrandStatus());
        tbBrandMapper.updateById(brand);

        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(request.getHeader("token")).getBody();
        String realName = body.get("realName", String.class);
        brand.setUpdateTime(new Date());
        brand.setCreateBy(realName);

        tbBrandMapper.updateById(brand);

        return Result.ok();
    }

    @Override
    public Result getBrandListAll() {
        List<TbBrand> tbBrands = tbBrandMapper.selectList(null);
        return Result.ok().setData(tbBrands);
    }
}




